/**
 * Class CustomCFONBOrderUploadModule.
 * 
 * @author Flavio Duboc
 */
CustomCFONBOrderUploadModule.inherits(OrderUploadModule);
function CustomCFONBOrderUploadModule() {
	this.params = {
			'uploadOrdersRequestPath' : 'uploadCustomCFONBFile',
			'searchUploadTasksRequestPath' : 'getCustomCFONBTasks',
			'downloadUploadTaskReportRequestPath' : 'downloadCustomCFONBTaskReport'
		};
	this.parentConstructor(OrderUploadModule);
}

CustomCFONBOrderUploadModule.neededRoles = ['UPLOAD_ORDER_CFONB'];

CustomCFONBOrderUploadModule.prototype.fillJQ = function(jQ) {
	var that = this;
	
	this.setParams(this.params);
	
	// Title
	jQ.append($('<h4>' + I18n.get('custom.cfonb.order.upload.title') + '</h4>'));
	
	// Order upload form
	var uploadForm = $('<div class="cropbottom well"></div>');
	jQ.append(uploadForm);
	var fileUpload = $('<div class="fileUpload"></div>');
	uploadForm.append(fileUpload);
	fileUpload.append(this.inputFileModule.buildJQ());
	fileUpload.append($('<span class="btn send">' + I18n.get('send') + '</span>').click(function() {
		that.uploadOrders();
	}));
	
	// Order search form
	jQ.append($('<h5>' + I18n.get('custom.cfonb.order.upload.search') + '</h5>'));
	var searchForm = $('<div class="well row-fluid"></div>');
	jQ.append(searchForm);
	
	var columnLeft = $('<div class="span6"></div>');
	columnLeft.append(this.inputStartDate.buildJQ());
	columnLeft.append(this.inputReferrer.buildJQ());
	var columnRight = $('<div class="span6"></div>');
	columnRight.append(this.inputEndDate.buildJQ());
	columnRight.append(this.inputStatus.buildJQ());
	searchForm.append(columnLeft).append(columnRight);

	// Buttons
	var buttonRow = $('<div class="btn-row"></div>');
	searchForm.append(buttonRow);
	
	var buttonSearch = $('<div class="btn search">' + I18n.get('search') + '</div>');
	buttonSearch.click(function() { that.searchUploadTasks(); });
	buttonRow.append(buttonSearch);
	
	// Order upload table
	var result = $('<div class="result"></div>');
	jQ.append(result);
	result.append($('<br/>'));
	result.append($('<h5>' + I18n.get('custom.cfonb.order.upload.list') + '</h5>'));
	if (this.uploadTable.objectMap.size == 0) {
		result.hide();
	}
	result.append(this.uploadTable.buildJQ());
	
	// Launches the search
	this.searchUploadTasks();
};

CustomCFONBOrderUploadModule.prototype.initUploadTable = function(jQ) {
	var that = this;
	
	var uploadTable = this.parentMethod(OrderUploadModule, "initUploadTable");
	//remove last column
	uploadTable.columnList.pop();
	
	//then re-add needed and additional column for download

	// Download XML
	var downloadCol = new Column('download', 'XML', '4%');
	uploadTable.columnList.push(downloadCol);
	downloadCol.getAlt = function(task) {
		if (task.status != 'STARTED') {
			return I18n.get('download.xml');
		} else {
			return I18n.get('download.notAvaiblable');
		}
	};
	downloadCol.getText = function(task) {
		if (task.status != 'STARTED') {
			return $('<i class="icon-download-alt icon"/>').click(function() { that.downloadUploadTaskReport(task.id, 'xml'); });
		}
		return '';
	};
	// Download CSV
	var downloadCol = new Column('download', 'CSV', '4%');
	uploadTable.columnList.push(downloadCol);
	downloadCol.getAlt = function(task) {
		if (task.status != 'STARTED') {
			return I18n.get('download.csv');
		} else {
			return I18n.get('download.notAvaiblable');
		}
	};
	downloadCol.getText = function(task) {
		if (task.status != 'STARTED') {
			return $('<i class="icon-download-alt icon"/>').click(function() { that.downloadUploadTaskReport(task.id, 'csv'); });
		}
		return '';
	};
	// Download CFONB
	var downloadCol = new Column('download', 'CFONB', '4%');
	uploadTable.columnList.push(downloadCol);
	downloadCol.getAlt = function(task) {
		if (task.status != 'STARTED') {
			return I18n.get('download.cfonb');
		} else {
			return I18n.get('download.notAvaiblable');
		}
	};
	downloadCol.getText = function(task) {
		if (task.status != 'STARTED') {
			return $('<i class="icon-download-alt icon"/>').click(function() { that.downloadUploadTaskReport(task.id, 'cfonb'); });
		}
		return '';
	};
	
	return uploadTable;
};

CustomCFONBOrderUploadModule.prototype.downloadUploadTaskReport = function(taskId, extension) {
	var that = this;
	AjaxManager.download(that.downloadUploadTaskReportRequestPath, {
		'taskId' : taskId,
		'extension' : extension
	}, function(){
		//if download failed, callback is called
		PopupModule.getInstance().show({
			'title' : I18n.get('CustomCFONB.dlError.title'),
			'content' : I18n.get('CustomCFONB.dlError.content'),
			'button0' : { 'text' : 'OK' }
		});
	});
};
